/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sdc.component.type;


import java.util.List;
import java.util.ArrayList;
import sdc.strategy.proposal.ProbMatrixStrategy;
import sdc.utility.MersenneTwisterRNG;

/**
 *
 * @author masa
 */
public class ComponentProbMatrixFactory {

    private double[][] probMatrix;

    public ComponentProbMatrixFactory() {
        probMatrix = null;
    }

    public ComponentProbMatrixFactory(double[][] probMatrix){
        int size = probMatrix[0].length;
        for(int i=0;i<size;i++){
            for(int j=0;j<size;j++){
                this.probMatrix[i][j] = probMatrix[i][j];
            }
        }
    }

    private void init(int n) {
        probMatrix = new double[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                probMatrix[i][j] = 0.0;
            }
        }
    }

    public void createProbMatrix(int typeNum) {
        List<Integer> usedList = new ArrayList<Integer>();
        List<Integer> freeList = new ArrayList<Integer>();
        ProbMatrixStrategy str = new ProbMatrixStrategy();
        this.init(typeNum);
        if(ComponentTypePool.INSTANCE.inNull()){
            usedList = str.CreateFirstUse(probMatrix, (typeNum + 1)/2);
        }
        else{
            //
            System.out.println("Erro:at createProbMatrixFactory");
        }
        freeList = str.createProb(usedList, probMatrix);
        str.createConnect(probMatrix, usedList, freeList);
    }

    private void setProb(int i, List<Integer> freeList){
        MersenneTwisterRNG rnd = MersenneTwisterRNG.INSTANCE;
        if(freeList.size() == 0)
            return;
        int tmp = freeList.get(rnd.nextInt(freeList.size()));
        if(probMatrix[i][tmp] == 0.0){
            probMatrix[i][tmp] = rnd.nextDouble();
            freeList.remove((Integer)tmp);
            setProb(tmp,freeList);
        }
        else
            return;
    }

    public double[][] getMatrix() {
        double probMatrix1[][] = {
                    {-1.0, 1.0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0,   0,   0,   0,   0, 1.0,   0,   0,   0,   0,   0},
                    {   0,   0,-1.0,   0,   0, 1.0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,-1.0,   0,   0, 1.0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0,   0,   0,   0,   0, 1.0,   0,   0,   0,   0,   0},
                    {   0, 1.0,   0,   0,   0,   0,-1.0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0, 1.0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0, 1.0,   0,   0,-1.0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                    {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0},
                };
        return probMatrix1;
    }
}
